
<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>

<script type="text/javascript" src="<%=jsPath%>/jquery/jquery.timers.js"></script><!--定时器-->
<script type="text/javascript" src="<%=jsPath%>/terminalCommand.js?v=1"></script><!--终端命令结果查询-->
<script type="text/javascript" src="<%=jsPath%>/videoPlayer.js"></script><!--终端命令结果查询-->


<script>

	function  startListen() {
	    var vehicleId = ${vehicleId};
	    var simNo = "${simNo}";
	    var channel = 1;
        var ret = window.parent.MyVideoPlayer.startListen(vehicleId, simNo, channel);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
			alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }

    }

    function onStopListen()
	{
        var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = 1;
        var ret = window.parent.MyVideoPlayer.stopListen(simNo);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            //this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
            alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }
	}

    function stopListen()
	{
	    var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = 1;
	    var url = "<%=ApplicationPath%>/videoCommand/stopListen.action";
	    var param = {vehicleId:vehicleId, simNo:simNo,channelId:channel};
	    $.getJSON(url, param, function(result)
		{
		    var message = result.success ? "停止成功": "停止失败";
		    $(".commandMsg").html(message);
		    if(result.success)
			{
                var commandId = result.data; //下发成功后，获取到命令Id
                TerminalCommand.commandButton = $("#btnStopListen");
                TerminalCommand.startQueryResult(commandId,null,onStopListen);//命令下发成功,根据命令id,开始尝试获取检索结果
			}

		})
	}

    function onCommandSuccess()
	{
	    startListen();
	}

    /**
	 * 当easyui的dialog 被关闭的时候，会自动调用此方法，停止监听
     * @returns {boolean}
     */
    function onWindowBeforeClose()
	{
	    try {

            stopListen();
            console.log("监听窗口被关闭，自动停止监听");
		}catch(e)
		{
		    console.log(e.message);
		}
	    return true;
	}

$().ready(function() {
	 $("#entityForm").validate(); //初始化验证信息

	$("#btnStopListen").click(function()
	{
	    stopListen();
	});

    var defaultChannelId = "1";//默认选中
    $("#channel").fillVehicleChannels({vehicleId:"${vehicleId}",selectedValue:defaultChannelId});
	 var defaultRoleId = "";//${roleId}; //取出当前车辆的车牌颜色，默认选中
	//ajax填充下拉框数据 填充监听类型 选项
	 $("#listenType").lookup({category:"ListenTerminal", selectedValue:1});
	  Utility.ajaxSubmitForm("entityForm", {
						 success:function(responseText)
						 {
							   var result = responseText;
								if (result.success) {
									 var commandId = result.data; //下发成功后，获取到命令Id
                                    TerminalCommand.commandButton = $("#btnStartListen");
									 TerminalCommand.startQueryResult(commandId,null,onCommandSuccess);//命令下发成功,根据命令id,开始尝试获取检索结果
								}
								else {
									$(".commandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
									//停止所有的在$('body')上定时器  
									$('body').stopTime ();  
								  }
						 }
	 });
});
</script>
 <BODY>
	<form id="entityForm" name="entityForm" 
			action='<%=ApplicationPath%>/videoCommand/listen.action' method="POST">
				
        <input type="hidden"  name="vehicleId"  id="vehicleId" value="${vehicleId}"/>
		<input type="hidden"  name="simNo"  id="simNo" value="${simNo}"/>
  <table width="100%"  class="TableBlock">
					<tbody><tr>
						<td colspan="2" style="font-weight: bold; background: #EFEFEF;" height="25">监听信息
						<span style="color:red;background:blue;">${message}</span>
						</td>
					</tr>

					<tr>
						<td align="right">通道
						:</td>
						<td>
							<select id="channel" name="channel">
							</select>
						</td>
					</tr>
					<tr>


						<td colspan=2 align="center">
							<button id="btnStartListen" type="submit" class="button green medium sendjson" ><i class="fa fa-phone" style="margin-right:3px;"></i> 开始监听</button>

							<button id="btnStopListen" type="button" class="button red medium " ><i class="fa fa-stop-circle" style="margin-right:3px;"></i> 停止监听</button>



							<span class="commandMsg"></span></td>
						
					</tr>

				
					
				</tbody></table>
 </BODY>
</HTML>
